package com.skywo.demo1.controller

import com.skywo.demo1.common.result.R
import com.skywo.demo1.common.result.RF
import com.skywo.demo1.controller.req.UserAddReq
import com.skywo.demo1.controller.req.UserEditReq
import com.skywo.demo1.controller.req.UserQueryReq
import com.skywo.demo1.controller.req.UserRegisterReq
import com.skywo.demo1.controller.vo.UserInfoVo
import com.skywo.demo1.service.UserService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.data.domain.Page
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest

/**
 * 用户管理接口
 *
 * @author zhang.rx
 * @date 2024/7/18
 */
@Api(tags = ["用户管理接口"])
@RestController
@RequestMapping("/user")
class UserController(private val userService: UserService) {

    /**
     * 新增用户
     * @return boolean
     */
    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    fun addUser(@RequestBody req: UserAddReq): R<Boolean> {
        return RF.success(userService.addUser(req))
    }

    /**
     * 获取当前登录用户
     */
    @ApiOperation("获取当前登录用户")
    @GetMapping("/getLoginUser")
    fun getLoginUser(request: HttpServletRequest): R<UserInfoVo> {
        return RF.success(userService.getLoginUser(request))
    }

    /**
     * 用户注册
     * @return 注册状态
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    fun userRegister(@RequestBody userRegisterReq: UserRegisterReq): R<Boolean> {
        return RF.success(userService.userRegister(userRegisterReq))
    }

    /**
     * 根据用户id获取用户信息
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getUser/{id}")
    fun getUserById(@PathVariable id: String): R<UserInfoVo> {
        return RF.success(userService.getUserById(id))
    }

    /**
     * 根据用户id编辑用户信息
     * @return boolean
     */
    @ApiOperation("编辑用户")
    @PutMapping("/editUser")
    fun editUser(@RequestBody req: UserEditReq): R<Boolean> {
        return RF.success(userService.editUser(req))
    }

    /**
     * 删除用户
     * @return boolean
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/removeUser/{id}")
    fun removeUser(@PathVariable id: String): R<Boolean> {
        return RF.success(userService.removeUser(id))
    }

    /**
     * 删除用户
     * @return boolean
     */
    @ApiOperation("批量删除用户")
    @DeleteMapping("/removeUserList")
    fun removeUserList(@RequestBody ids: List<String>): R<Boolean> {
        return RF.success(userService.removeUserList(ids))
    }

    /**
     * 分页查询用户信息
     * @return 用户信息列表
     */
    @ApiOperation("分页查询用户信息")
    @PostMapping("/getUserList")
    fun pageUser(
        @RequestBody userQueryReq: UserQueryReq,
        @RequestParam(value = "pageNum", defaultValue = "1") pageNum: Int,
        @RequestParam(value = "pageSize", defaultValue = "10") pageSize: Int
    ): R<Page<UserInfoVo>> {
        return RF.success(userService.pageUser(userQueryReq, pageNum, pageSize))
    }

}